package com.qvtu.springboot.service;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qvtu.springboot.entity.Account;
import com.qvtu.springboot.entity.Employee;
import com.qvtu.springboot.exception.CustomException;
import com.qvtu.springboot.mapper.EmployeeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

//通过@service标注未springboot的bean
@Service
public class EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;

    public void add(Employee employee) {
        String username = employee.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        String no=employee.getNo();
        Employee dbEmployeeNo=employeeMapper.selectByNo(no);
        if (dbEmployee != null) {
            throw new CustomException("500", "账号已经存在，请更换其他账户");
        }
        if(dbEmployeeNo!=null){
            throw new CustomException("500","工号已经存在,请重新输入");
        }
        if (StrUtil.isBlank(employee.getPassword())) {//密码没填
//            设置默认密码123
            employee.setPassword("123");
        }
        if (StrUtil.isBlank(employee.getName())) {
            employee.setName(employee.getUsername());
        }
//        一定要设置角色
        employee.setRole("EMP");//角色为员工
        employeeMapper.insert(employee);
    }

    public Employee selectById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }

    public List<Employee> selectAll(Employee employee) {
        List<Employee> list = employeeMapper.selectAll(employee);
        return list;
    }

    //    分页查询
    public PageInfo<Employee> selectPage(Employee employee, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeMapper.selectAll(employee);
        return PageInfo.of(list);
    }

    //删除数据
    public void deleteById(Integer id) {
        employeeMapper.deleteById(id);
    }

    //    修改数据
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    //    登录逻辑
    public Employee login(Account account) {
        String username = account.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee == null) {
//            未查询到该用户
            throw new CustomException("500", "账号不存在");
        }
//        查询到用户
        String password = account.getPassword();
//        判断根据用户名查询到的用户信息的密码和你输入的密码是否一致
        if (!dbEmployee.getPassword().equals(password)) {
            throw new CustomException("500", "账号或密码错误");
        }
        return dbEmployee;
    }

    //注册
    public void register(Employee employee) {
        this.add(employee);
    }

    public void updatePasswprd(Account account) {
        Integer id = account.getId();
        Employee employee = this.selectById(id);
        if(employee.getPassword().equals(account.getPassword())){
            employee.setPassword(account.getNewPassword());//设置新密码
            employeeMapper.update(employee);//更新员工数据
        }else {
            throw new CustomException("500","输入的原密码错误");
        }

    }
}
